﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.Database;
using System.Data;
public partial class carinsurance_QuoteResultPrintPreview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            lblDateTime.Text = DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss tt");

            if (Request.Params["custid"] != null && Request.Params["carid"] != null)
            {
                string custId = Request.Params["custid"].ToString();
                string carId = Request.Params["carid"].ToString();

                Int64 _custId = 0;
                Int64 _carId = 0;

                if (Int64.TryParse(custId, out _custId) && Int64.TryParse(carId, out _carId))
                {
                    MyDatabase _db = new MyDatabase();
                    //Customer Details
                    string _strSelect = "select P.FirstName, P.SurName, C.Email, C.Mobile, P.DOB, P.Gender, M.Description MaritalStatus, O.Description Occupation, P.JobNature, P.YearDriveSg from tblCEUsrProfile P "
										+ "INNER JOIN tblCEContact C ON P.RefCEUserID = C.RefCEUserID "
                                        + "INNER JOIN tblMaritalStatusInfo M ON P.MaritalStatus = M.MaritalStatusId "
                                        + "INNER JOIN tblOccupationInfo O ON P.Occupation = O.OccupationId "
                                        + "Where P.RefCEUserId = " + custId;
                    DataTable _dtSelect = new DataTable("dtSelect");
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblCustomerName.Text = _dtSelect.Rows[0]["SurName"].ToString() + " " + _dtSelect.Rows[0]["FirstName"].ToString();
                        lblCustomerContactNo.Text = _dtSelect.Rows[0]["Mobile"].ToString();
                        lblCustomerEmail.Text = _dtSelect.Rows[0]["Email"].ToString();
                        lblMaritalStatus.Text = _dtSelect.Rows[0]["MaritalStatus"].ToString();
                        lblGender.Text = _dtSelect.Rows[0]["Gender"].ToString();
                        lblDOB.Text = Convert.ToDateTime(_dtSelect.Rows[0]["DOB"].ToString()).ToString("dd/MMM/yyyy");
                        lblOccupation.Text = _dtSelect.Rows[0]["Occupation"].ToString();
                        lblJobNature.Text = _dtSelect.Rows[0]["JobNature"].ToString();
                        lblDriveInSG.Text = _dtSelect.Rows[0]["YearDriveSg"].ToString();
                    }
                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");

                    _strSelect = " Select CD.MakeModelDesc, CD.RegYear, CD.IsPeak, CD.Info17, P.CompanyName CurrentInsurer from tblCarDetail CD"
                                + " INNER JOIN tblPreInsurer P ON CAST(CD.Info1 AS INT) = P.PreInsurerId"
                                + " Where P.CompanyType = 1 And P.ServiceType = 100 AND CD.RefCEUserId = " + custId + " And CD.CECarId= " + carId;
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblCarMakeModel.Text = _dtSelect.Rows[0]["MakeModelDesc"].ToString();
                        if (lblCarMakeModel.Text.Length > 30)
                            lblCarMakeModel.Font.Size = new FontUnit(8);
                        else
                            lblCarMakeModel.Font.Size = new FontUnit(9);
                        lblOffpeak.Text = _dtSelect.Rows[0]["IsPeak"].ToString() == "1" ? "Yes" : "No";
                        lblRegYear.Text = _dtSelect.Rows[0]["RegYear"].ToString();
                        lblCurrentInsurer.Text = _dtSelect.Rows[0]["CurrentInsurer"].ToString();

                        if (_dtSelect.Rows[0]["Info17"] != DBNull.Value && _dtSelect.Rows[0]["Info17"].ToString().Length > 0)
                        {
                            lblVehicleNo.Text = _dtSelect.Rows[0]["Info17"].ToString();
                        }
                        else
                        {
                            lblVehicleNo.Text = "N/A";
                        }
                    }

                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");
                    bool _IsClaim = false;
                    bool _IsAddDriver = false;

                    _strSelect = "Select CoverageCommenceDate, Info1 AS NCDPercent, IsClaimLastThreeYear, IsOffDriveCom, IsAddDriver, NoOfDriver from tblCarPolicy"
                        + " Where RefCEUserId = " + custId + " And CECarId= " + carId;
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblNCD.Text = _dtSelect.Rows[0]["NCDPercent"].ToString() == "-1" ? "0" : _dtSelect.Rows[0]["NCDPercent"].ToString() + "%";
                        lblOFD.Text = Convert.ToBoolean(_dtSelect.Rows[0]["IsOffDriveCom"].ToString()) == true ? "Yes" : "No";
                        _IsClaim = Convert.ToBoolean(_dtSelect.Rows[0]["IsClaimLastThreeYear"].ToString());
                        _IsAddDriver = Convert.ToBoolean(_dtSelect.Rows[0]["IsAddDriver"].ToString());
                        if (_IsAddDriver == true)
                        {
                            lblDriverCount.Text = _dtSelect.Rows[0]["NoOfDriver"].ToString();
                        }
                        else
                        {
                            lblDriverCount.Text = "0";
                        }

                        DateTime _dtNewCommDate = Convert.ToDateTime(_dtSelect.Rows[0]["CoverageCommenceDate"].ToString());
                        if (DateTime.Compare(_dtNewCommDate, DateTime.Now) >= 0)
                        {
                            lblNewCommDate.Text = "<b style='color:green;'>" + _dtNewCommDate.ToString("dd/MMM/yyyy") + "</b>";
                        }
                        else
                        {
                            lblNewCommDate.Text = "<b style='color:red;'>" + _dtNewCommDate.ToString("dd/MMM/yyyy") + "</b>";
                        }
                    }

                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");


                    if (_IsClaim)
                    {
                        _strSelect = "select (NOCLast3 + NOCLast2 + NOCLast1) ClaimCount, (Cast(NOCLast1Value as float) + Cast(NOCLast2Value as float) + Cast(NOCLast3Value as float)) ClaimAmount from tblCarClaimHistory"
                        + " Where RefCEUserId = " + custId + " And CECarId= " + carId;
                        _dtSelect = _db.GetDataTable(_strSelect);
                        if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                        {
                            lblClaimInfo.Text = "Yes (Count: " + _dtSelect.Rows[0]["ClaimCount"].ToString() + " / Amount: " + Convert.ToDouble(_dtSelect.Rows[0]["ClaimAmount"].ToString()).ToString("$#,000.00") + ")";
                        }
                    }
                    else
                    {
                        lblClaimInfo.Text = "No Claim";
                    }
                }
            }
        }
    }
}
